<?php

namespace app\controller\gas;

use app\BaseController;
use app\model\StationBasic;
use app\model\StationChange;
use app\model\StationOrder;
use think\Response;

class Order extends BaseController
{
    public function lst(): Response
    {
        return $this->success($this->app->make(StationOrder::class)->lst());
    }

    public function download(): Response
    {
        return $this->success($this->app->make(StationOrder::class)->download());
    }

    public function worker(): Response
    {
        $station = $this->request->station;
        //获取最近第一条交班数据
        $model = $this->app->make(StationChange::class);
        $end_time = $model->where('station_id', $station['id'])->order('end_time', 'desc')->value("end_time");
        $timer = date("Y-m-d H:i:s");
        if (empty($end_time)) $end_time = $station['create_time'];
        //获取当前时间段的订单信息
        $order = $this->app->make(StationOrder::class);
        $payment = $order->where('station_id', $station['id'])->where('payment_time', 'BETWEEN', [$end_time, $timer])->where('state', 'in', [1, 2])->field("oil,sum(money) money,sum(merchant_money) merchant,count(oil) total,sum(litter) litter")->group("oil")->select()->toArray();
        $payment_total = array_merge([
            "total" => 0,
            "money" => 0,
            "litter" => 0
        ], array_column($payment, null, "oil"));
        $refund = $order->where('station_id', $station['id'])->where('cancel_time', 'BETWEEN', [$end_time, $timer])->where('state', 'in', [3, 4, 5])->field("oil,sum(money) money,sum(merchant_money) merchant,count(oil) total,sum(litter) litter")->group("oil")->select()->toArray();
        $refund_total = array_merge([
            "total" => 0,
            "money" => 0,
            "litter" => 0
        ], array_column($refund, null, "oil"));
        $model->exists(false)->save([
            "station_id" => $station['id'],
            "start_time" => $end_time,
            "end_time" => $timer,
            "payment_text" => $payment_total,
            "refund_text" => $refund_total
        ]);
        return $this->success();
    }

    public function workLst(): Response
    {
        return $this->success($this->app->make(StationChange::class)->lst());
    }

    public function order_staic()
    {
        $station = $this->request->station;
        $param = $this->request->param();
        $time = time();
        $last_week = strtotime("-1 week", $time);
        $todayTotal = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00"), date("Y-m-d H:i:s")])->when(!empty($station),function ($query) use($station){
            $query->where("station_id",$station['id']);
        })->sum("money");
        $lastTotal = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00", $last_week), date("Y-m-d H:i:s", $last_week)])->when(!empty($station),function ($query) use($station){
            $query->where("station_id",$station['id']);
        })->sum("money");
        $chatTotal = bcsub($todayTotal, $lastTotal, 2);
        $tip = $chatTotal > 0 ? 'up' : ($chatTotal < 0 ? 'down' : 'over');
        $todayOrder = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00"), date("Y-m-d H:i:s")])->when(!empty($station),function ($query) use($station){
            $query->where("station_id",$station['id']);
        })->field("money,payment_time")->select()->toArray();
        $lastOrder = (new StationOrder)->where('state', 1)->where('payment_time', 'between', [date("Y-m-d 00:00:00", $last_week), date("Y-m-d H:i:s", $last_week)])->when(!empty($station),function ($query) use($station){
            $query->where("station_id",$station['id']);
        })->field("money,payment_time")->select()->toArray();
        $items = [
            [
                "name" => "今日流水",
                "data" => array_fill(0, (int)date("H") + 1, 0)
            ],
            [
                "name" => "上周流水",
                "data" => array_fill(0, (int)date("H") + 1, 0)
            ],
        ];
        if (!empty($todayOrder)) {
            foreach ($todayOrder as $k => &$v) {
                $h = (int)date("H", strtotime($v['payment_time']));
                $items[0]['data'][$h] += $v['money'];
            }
        }

        if (!empty($lastOrder)) {
            foreach ($lastOrder as $k => &$v) {
                $h = (int)date("H", strtotime($v['payment_time']));
                $items[1]['data'][$h] += $v['money'];
            }
        }

        return $this->success(
            ["data" => $items, "today" => $todayTotal, 'last' => $lastTotal, "tip" => $tip, "chat" => abs($chatTotal), "key" => array_map(function ($v) {
                return ($v < 10 ? "0$v" : $v) . ":00";
            }, array_keys($items[1]['data']))]
        );
    }
}
